package alpha.com.google.code.mechanic.janitor.ui.handler;

import java.io.IOException;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import alpha.com.google.code.mechanic.janitor.controller.ContextData;
import alpha.com.google.code.mechanic.janitor.controller.MechanicTaskSource;
import alpha.com.google.code.mechanic.janitor.model.ContextsCollector;
import alpha.com.google.code.mechanic.janitor.model.IContext;

/**
 * 
 * This class is to handle the commands in the drop down list of the Janitor
 * pull down button in the Eclipse main toolbar.
 * 
 * Last_edited: Nov 3, 2014
 *
 * @History
 * 1. Change the method used to save the Contexts information to context.con
 * file.
 * 1) In the method Object execute(ExecutionEvent event), use the method
 * SaveContextsInfo() of ContextData to replace the method
 * serializeContextsInfo(File file) of ContextsCollector.
 * @author jingjiejiang
 * @Edited Nov 3, 2014
 *
 * @author jingjiejiang
 * @since Sep 22, 2014
 */
public class SwitchContextHandler extends AbstractHandler {
  
  private MechanicTaskSource taskSrc = new MechanicTaskSource();
  private ContextData ctxData = new ContextData();
  private ContextsCollector ctxCollector = new ContextsCollector();

  public Object execute(ExecutionEvent event) throws ExecutionException {
    
    Object trigger = event.getTrigger();
    String cmdlabel = getCommandLabel(trigger.toString());
    
    if (cmdlabel.equals("Default Context")) {
      taskSrc.removeAllCtxDirFromTaskSrc();
      ctxData.getContextsCollector().clearContextSelection();
      
      // write the changes back to the context.con file
      try {
        ctxData.saveContextsInfo();
      } catch (IOException e) {
        Shell pshell = Display.getCurrent().getActiveShell();
        MessageDialog.openError(pshell, "Couldn't save the Contexts", e.getMessage());
      }
    }
    else {
      
      String ctxdir = "";
      try {
        ctxdir = getContextLocation(cmdlabel);
      } catch (Exception error) {
        Shell pshell = Display.getCurrent().getActiveShell();
        MessageDialog.openError(pshell, "Couldn't read the Context data."
            , error.getMessage());
      }
      
      
      if (false == ctxdir.equals("")) {
        taskSrc.removeAllCtxDirFromTaskSrc();
        taskSrc.addDirectoryToTaskSrc(ctxdir);
        
        // write the changes back to the context.con file
        try {
          ctxData.saveContextsInfo();
        } catch (IOException e) {
          Shell pshell = Display.getCurrent().getActiveShell();
          MessageDialog.openError(pshell, "Couldn't save the Contexts", e.getMessage());
        }
      }
    }
    
    return null;
  }
  
  /**
   * Get the label of the command that is triggered.
   *
   * @param trigger the string information of the trigger of the event.
   * @return return the label of the command.
   */
  private String getCommandLabel (String trigger) {
    
    int beginindex = trigger.indexOf("{", trigger.indexOf("{") + 1);
    int endindex = trigger.indexOf("}");
    
    return trigger.substring(beginindex + 1, endindex);
  }
  
  /**
   * Get the directory of the Context that is selected.
   *
   * @param ctxname name of the Context.
   * @return return the location of the Context.
   * @throws IOException 
   */
  private String getContextLocation (String ctxname) throws Exception {
    
    ctxData.getContextsCollector().resetContextList();
    
    try {
      ctxData.readContextsFile();
    } catch (Exception error) {
      throw error;
    }
    
    ctxCollector = ctxData.getContextsCollector();

    IContext context = ctxCollector.getContextFromName(ctxname);
    if (context != null) {
      
      ctxCollector.clearContextSelection();
      context.setSelected(true);
      return context.getFileLocation();
    }
    else {
      return "";
    }
  }

}
